Skip to content

Fix credential profile overwrite in config.toml#474

Open
vivienhenz24 wants to merge 2 commits intorunpod:mainfrom
vivienhenz24:fix/data_loss
Open

Fix credential profile overwrite in config.toml#474
vivienhenz24 wants to merge 2 commits intorunpod:mainfrom
vivienhenz24:fix/data_loss

Conversation

@vivienhenz24
Copy link

Found a data‑loss bug in set_credentials() where it opens config.toml in "w" mode and writes only the current profile, which truncates the file and deletes all other profiles. That means if you already have [default] and [profile1], calling set_credentials("new-key", "profile2") leaves you with only [profile2] and no warning.

I switched it to read the existing TOML, update/add the requested profile in memory, and write the whole thing back so nothing else is lost, and adjusted the test to match.

@deanq deanq requested a review from Copilot March 18, 2026 22:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a data-loss issue in the CLI config credential writer by preserving existing profiles in ~/.runpod/config.toml instead of overwriting the entire file when adding a profile.

Changes:

  • Updated set_credentials() to read/parse the existing TOML, update/add the target profile, and write the full config back.
  • Switched the credential-writing implementation to use tomlkit for parsing/writing.
  • Updated the unit test to reflect the new read-then-write flow.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
runpod/cli/groups/config/functions.py Loads existing TOML config, updates/creates the requested profile, and dumps the full document back to disk.
tests/test_cli/test_cli_groups/test_config_functions.py Adjusts set_credentials() test to expect a read + write cycle and tomlkit.dump() usage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +43 to +45
raise ValueError(
"Profile already exists. Use `update_credentials` instead."
)
Comment on lines 49 to +50
with open(CREDENTIAL_FILE, "w", encoding="UTF-8") as cred_file:
cred_file.write("[" + profile + "]\n")
cred_file.write('api_key = "' + api_key + '"\n')
tomlkit.dump(config, cred_file)
Comment on lines +24 to 26
mock_document.side_effect = [{}, {"default": True}]
functions.set_credentials("RUNPOD_API_KEY")

Comment on lines +19 to +39
@patch("builtins.open", new_callable=mock_open, read_data="")
def test_set_credentials(self, mock_file, mock_document, mock_dump):
"""
Tests the set_credentials function.
"""
mock_toml_load.return_value = ""
mock_document.side_effect = [{}, {"default": True}]
functions.set_credentials("RUNPOD_API_KEY")

mock_file.assert_called_with(functions.CREDENTIAL_FILE, "w", encoding="UTF-8")
assert any(
call.args[0] == functions.CREDENTIAL_FILE
and call.args[1] == "r"
and call.kwargs.get("encoding") == "UTF-8"
for call in mock_file.call_args_list
)
assert any(
call.args[0] == functions.CREDENTIAL_FILE
and call.args[1] == "w"
and call.kwargs.get("encoding") == "UTF-8"
for call in mock_file.call_args_list
)
assert mock_dump.called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants